package com.mxprojects.io
{
	import flash.net.SharedObject;
	
	public class BoardCookie
	{
		private static const USER_COOKIE_NAME:String = "userprefs";
		private static var so:SharedObject = SharedObject.getLocal(USER_COOKIE_NAME);	
		
		public function BoardCookie(){}
		
		public static function loadCookie():void {
			if(so.size != 0){
				//login the user
				UserControl.login(so.data.username, so.data.password, true);
				flushAndClose();			
			}else{
				//show the default information
				MXP3.header.showDefaultUserInfo();
				
				//setup the deep linking and load the forum
				DeepLinking.setup();
			}
		}
		
		public static function saveCookie(username:String, password:String, userID:int):void {
			setUser(username);
			setPassword(password);
			setUserID(userID);
		}
		
		public static function clearCookie():void {
			so.clear();
		}
		
		public static function setUser(username:String):void {
			so.data.username = username;
			flushAndClose();
		}
		
		public static function setPassword(md5Password:String):void {
			so.data.password = md5Password;
			flushAndClose();
		}
		
		public static function setUserID(userID:int):void {
			so.data.userID = userID;
			flushAndClose();
		}
		
		public static function getUser():String {
			return so.data.username;
		}
		
		public static function getPassword():String {
			return so.data.password;
		}
		
		public static function getUserID():String {
			return so.data.userID;
		}
		
		private static function flushAndClose():void {
			so.flush();
			so.close();
		}
	}
}